﻿#include "formtestinfo.h"
#include "ui_formtestinfo.h"
#include "globalstatic.h"
#include "globalsymbol.h"

#include <QDebug>
#include <QDateTime>

FormTestInfo::FormTestInfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormTestInfo)
{
    ui->setupUi(this);

    // 初始化
    initUI();
    init();
}

FormTestInfo::~FormTestInfo()
{
    delete ui;
}

void FormTestInfo::clear()
{
    ui->lblItemName->setText(QStringLiteral("测试项目:"));
    ui->lblSimuPoints->setText(QStringLiteral("已采集点数:"));
    ui->lblTotalPoints->setText(QStringLiteral("总点数:"));
    ui->lblRunTime->setText(QStringLiteral("当前项目运行时间:"));
    ui->lblTheoryRunTime->setText(QStringLiteral("当前项目理论运行时间:"));
}

void FormTestInfo::setSimuInterval(const int simuInterval)
{
    m_simuInterval = simuInterval;
}

void FormTestInfo::setItemName(const QString &itemName)
{
    ui->lblItemName->setText(QStringLiteral("测试项目: %1").arg(itemName));
}

void FormTestInfo::setTheroyRunTime(const int &theoryRunTime)
{
    // 根据理论运行时间计算点数
    ui->lblTotalPoints->setText(QStringLiteral("总点数: %1").arg(theoryRunTime / m_simuInterval));
    ui->lblTheoryRunTime->setText(QStringLiteral("当前项目理论运行时间: %1").arg(QString::number(theoryRunTime / 1000., 'f', 3)));
    ui->lblSimuPoints->setText(QStringLiteral("已采集点数:"));
    ui->lblRunTime->setText(QStringLiteral("当前项目运行时间:"));
}

void FormTestInfo::updateProgress(const int &simuPoints, const qint64 &runTime)
{
    // 更新流程进度
    ui->lblSimuPoints->setText(QStringLiteral("已采集点数: %1").arg(simuPoints));
    ui->lblRunTime->setText(QStringLiteral("当前项目运行时间: %1").arg(QString::number(runTime / 1000., 'f', 3)));
}

void FormTestInfo::finishProgress(const int &simuPoints, const qint64 &runTime)
{
    // 流程进行完成
    ui->lblSimuPoints->setText(QStringLiteral("已采集点数: %1").arg(simuPoints));
    ui->lblRunTime->setText(QStringLiteral("当前项目运行时间: %1").arg(QString::number(runTime / 1000., 'f', 3)));
}

void FormTestInfo::updateTrainStart()
{
    ui->lblTrainStartTime->setText(QStringLiteral("开始时间: %1").arg(QTime::currentTime().toString("HH:mm:ss")));
    ui->lblTrainEndTime->setText(QStringLiteral("结束时间: "));
    ui->lblTrainStatus->setPixmap(m_greenPixmap);
}

void FormTestInfo::updateTraining()
{
    qint64 timeSpan = QDateTime::currentSecsSinceEpoch() - GlobalStatic::trainStartTime();
    int second = timeSpan % 60;
    int minute = timeSpan / 60;
    int hour = timeSpan / 3600;
    ui->lblTrainingTime->setText(QStringLiteral("已进行时间: %1:%2:%3").arg(hour).arg(minute).arg(second));
}

void FormTestInfo::updateTrainEnd()
{
    ui->lblTrainEndTime->setText(QStringLiteral("结束时间: %1").arg(QTime::currentTime().toString("HH:mm:ss")));
    ui->lblTrainStatus->setPixmap(m_grayPixmap);
}

void FormTestInfo::updateStepInfo(const QString &stepCode)
{
    QString stepType = QStringLiteral("日常练习");
    if (GlobalStatic::training())
        stepType = QStringLiteral("训练考核");
    QString stepInfo = QStringLiteral("%1: [%2] | %3").arg(QTime::currentTime().toString("HH:mm:ss"), stepType, GlobalStatic::getTrainStep(stepCode));
    m_stepInfoModel.insertRow(m_stepInfoModel.rowCount());
    QModelIndex modelIndex = m_stepInfoModel.index(m_stepInfoModel.rowCount() - 1, 0);
    m_stepInfoModel.setData(modelIndex, stepInfo, Qt::DisplayRole);
    ui->lvwStepInfo->setCurrentIndex(modelIndex);

    if (m_stepInfoModel.rowCount() > STEP_INFO_COUNT) {
        // 超过最大行数, 删除最早的
        modelIndex = m_stepInfoModel.index(0, 0);
        m_stepInfoModel.removeRow(modelIndex.row());
    }
}

void FormTestInfo::initUI()
{
    // 训练考试指示灯
    m_grayPixmap.load(":res/image/gray.png");
    m_greenPixmap.load(":res/image/green.png");
    ui->lblTrainStatus->setPixmap(m_grayPixmap);
    ui->lblTrainStatus->setScaledContents(true);

    // 绑定模型
    ui->lvwStepInfo->setModel(&m_stepInfoModel);
    // 聚焦方式
    //ui->lvwStepInfo->setSelectionMode(QAbstractItemView::NoSelection);
    //ui->lvwStepInfo->setFocusPolicy(Qt::NoFocus);
    // 只读
    ui->lvwStepInfo->setEditTriggers(QListView::NoEditTriggers);
}

void FormTestInfo::init()
{
}


